package com.lemon.smartsafetynew.ui.adapter.message;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.message.MessageVo;
import com.lemon.smartsafetynew.entity.patrol.SafePatrolBo;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.hidden.SafeHiddenUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * 推送消息记录适配器
 * Created by Andrew on 2018/4/23.
 */

public class MessageListAdapter extends LemonRcvAdapter<MessageVo> {

    private RecyclerTClickListener<MessageVo> mListener;

    public MessageListAdapter(@Nullable List<MessageVo> data, RecyclerTClickListener<MessageVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new Item();
    }

    public class Item implements AdapterItem<MessageVo> {

        @Bind(R.id.message_type_tv)
        TextView messageTypeTv;
        @Bind(R.id.message_layout)
        LinearLayout messageLayout;
        @Bind(R.id.message_type_iv)
        ImageView messageTypeIv;
        @Bind(R.id.tv_notice_time)
        TextView tvNoticeTime;
        @Bind(R.id.message_company_tv)
        TextView messageCompanyTv;
        @Bind(R.id.ll_hiddden)
        LinearLayout llHiddden;
        @Bind(R.id.message_source_tv)
        TextView messageSourceTv;
        @Bind(R.id.message_code_tv)
        TextView messageCodeTv;
        @Bind(R.id.tv_hidden_people)
        TextView tvHiddenPeople;
        @Bind(R.id.message_people_tv)
        TextView messagePeopleTv;
        @Bind(R.id.danger_name_tv)
        TextView dangerNameTv;
        @Bind(R.id.danger_level_tv)
        TextView dangerLevelTv;
        @Bind(R.id.ll_check)
        LinearLayout llCheck;
        @Bind(R.id.equipment_name_tv)
        TextView equipmentNameTv;
        @Bind(R.id.out_conpany_tv)
        TextView outConpanyTv;
        @Bind(R.id.ll_expire)
        LinearLayout llExpire;
        @Bind(R.id.tv_item_danger)
        TextView tvItemDanger;
        @Bind(R.id.tv_item_grid_name)
        TextView tvItemGridName;
        @Bind(R.id.warning_company_tv)
        TextView warningCompanyTv;
        @Bind(R.id.warning_location_tv)
        TextView warningLocationTv;
        @Bind(R.id.warning_reason_tv)
        TextView warningReasonTv;
        @Bind(R.id.warning_ll)
        LinearLayout warningLl;

        private MessageVo data;
        private int pos;

        @Override
        public int getLayoutResId() {
            return R.layout.item_message_recycler_view;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {

            messageLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(data, pos);
                }
            });

        }

        @Override
        public void handleData(MessageVo messageVo, int position) {

            data = messageVo;
            pos = position;
            int msgType = messageVo.getMsg_type();
            int formType = messageVo.getInform_type();
            messageTypeTv.setText(SafeHiddenUtils.getHiddenContentType(formType + "") +
                    CommonUtil.avoidNullString(messageVo.getMsg_content()));
            switch (formType) {
                case 1:  //隐患通知
                    llCheck.setVisibility(View.GONE);
                    llExpire.setVisibility(View.GONE);
                    llHiddden.setVisibility(View.VISIBLE);
                    warningLl.setVisibility(View.GONE);
                    messageTypeIv.setImageResource(R.mipmap.hidden_danger_icon);
                    tvHiddenPeople.setText(CommonUtil.avoidNullString(messageVo.getAddUserType()));
                    messageCompanyTv.setText(CommonUtil.avoidNullString(messageVo.getCompanyName()));
                    messageSourceTv.setText(CommonUtil.avoidNullString(messageVo.getSourceType()));
                    messageCodeTv.setText(CommonUtil.avoidNullString(messageVo.getHiddenDangerCode()));
                    messagePeopleTv.setText(CommonUtil.avoidNullString(messageVo.getAdd_user_name()));
                    break;
                case 2:  //到期提醒 {特种设备，特种作业操作证}

                    llCheck.setVisibility(View.GONE);
                    llExpire.setVisibility(View.VISIBLE);
                    llHiddden.setVisibility(View.GONE);
                    warningLl.setVisibility(View.GONE);
                    messageTypeIv.setImageResource(R.mipmap.expire_icon);
                    if (TextUtils.isEmpty(messageVo.getEquipmentName())) {
                        equipmentNameTv.setText("");
                    } else {
                        equipmentNameTv.setText(messageVo.getEquipmentName());
                    }
                    outConpanyTv.setText(CommonUtil.avoidNullString(messageVo.getFactoryNumber()));
                    break;
                case 3:  //巡查通知
                    llCheck.setVisibility(View.VISIBLE);
                    llExpire.setVisibility(View.GONE);
                    llHiddden.setVisibility(View.GONE);
                    warningLl.setVisibility(View.GONE);
                    messageTypeIv.setImageResource(R.mipmap.check_icon);
                    String type = CommonUtil.avoidNullString(messageVo.getItemType());
                    if (SafePatrolBo.ItemType.grid_check_point.getValue().equals(type)) {
                        tvItemDanger.setText(R.string.belong_company);
                        tvItemGridName.setText(R.string.grid_name);
                        dangerNameTv.setText(CommonUtil.avoidNullString(messageVo.getCompanyName()));
                        dangerLevelTv.setText(CommonUtil.avoidNullString(messageVo.getItemName()));
                    } else if (SafePatrolBo.ItemType.self_check_point.getValue().equals(type)) {
                        tvItemDanger.setText(R.string.item_danger_name);
                        tvItemGridName.setText(R.string.danger_level);
                        dangerNameTv.setText(CommonUtil.avoidNullString(messageVo.getItemName()));
                        dangerLevelTv.setText(CommonUtil.avoidNullString(messageVo.getRiskLevel()));
                    }
                    break;
                case 4:  //        气火灾，高危工艺，储罐监测，可燃/有毒气体}
                    messageTypeIv.setImageResource(R.mipmap.warning_icon);
                    llCheck.setVisibility(View.GONE);
                    llExpire.setVisibility(View.GONE);
                    llHiddden.setVisibility(View.GONE);
                    warningLl.setVisibility(View.VISIBLE);

                    warningCompanyTv.setText(CommonUtil.avoidNullString(messageVo.getCompanyName()));
                    warningLocationTv.setText(CommonUtil.avoidNullString(messageVo.getLocationArea()));
                    warningReasonTv.setText(CommonUtil.avoidNullString(messageVo.getReason()));

                    break;
            }
            tvNoticeTime.setText(CommonUtil.getFriendTimeStr(messageVo.getAdd_time()));
        }
    }
}
